package by.msokoloff.javaweb2015.webapp.page.airport;

import javax.inject.Inject;

import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.IModel;

import by.msokoloff.javaweb2015.datamodel.Airport;
import by.msokoloff.javaweb2015.datamodel.City;
import by.msokoloff.javaweb2015.services.AirportService;
import by.msokoloff.javaweb2015.services.CityService;
import by.msokoloff.javaweb2015.webapp.page.EntityListPage;
import by.msokoloff.javaweb2015.webapp.utils.renderer.CityChoiceRenderer;

public class AirportForm extends Form<Airport> {

	@Inject
	private AirportService airportService;

	@Inject
	private CityService cityService;

	private Airport airport;

	public AirportForm(String id, IModel<Airport> model) {
		super(id, model);
		add(new Label("id"));
		TextField<String> nameTextField = new TextField<String>("name");
		nameTextField.add(new PropertyValidator<String>());
		add(nameTextField);
		TextField<String> iataTextField = new TextField<String>("iataCode");
		iataTextField.add(new PropertyValidator<String>());
		add(iataTextField);
		TextField<String> taxesTextField = new TextField<String>("taxes");
		taxesTextField.add(new PropertyValidator<String>());
		add(taxesTextField);
		add(new DropDownChoice<City>("city", cityService.getAll(), CityChoiceRenderer.INSTANCE));
		add(new FeedbackPanel("feedbackpanel"));
		airport = model.getObject();
	}

	@Override
	public final void onSubmit() {
		airportService.createOrUpdate(airport);
		setResponsePage(new EntityListPage(Airport.class));

	}

	@Override
	public final void onError() {
		System.out.println("Error!");
	}

}
